 var path = require('path');

 module.exports = function (UC, source, config, ArFor, cache, package) {

     var source = config.Source;

     var UserControl = UC;

     var openjson = config.openjson;

     var source = config.Source;

     var installok = function (mid, body, Callback) {
         new source(mid, body, function (obj) {
             Callback()
         })
     };

     var getpathjoin = function (_path, file) {
         var u_path = path.join(_path, file);
         var __path = path.join(config.Files.Plugin, '../');
         if (__path.indexOf(u_path) > -1) {
             return u_path;
         } else {
             return u_path;
         }
     };

     var unInstallPlugin = function (obj, _Callback) {
         var localpath = getpathjoin(config.Files.Plugin, obj.localpath);
         var packagepath = getpathjoin(localpath, 'package.json');
         openjson(packagepath, function (error, package) {
             if (error != null) {
                 _Callback(error);
             } else {
                 var _package = config.installPackages[obj.key];
                 config.unInstallApp(packagepath, _package, function () {
                     _Callback(null, _package);
                 })
             }
         })
     };
     var unInstallApp = function (obj, body) {
         var setMessage = function (val) {
             var socketid = UserControl.Userid2Socketid(body.user_id);
             if (socketid) {
                 var Socket = config.Socket;
                 Socket.SetClientMessage(socketid, val)
             };
         }
         unInstallPlugin(obj, function (error, db) {
             if (error == null) {
                 var value = {
                     id: body.id,
                     error: 0,
                     "key": "un-install-plugin",
                     notify: {
                         duration: 0,
                         title: '@plugin_uninstall_successfully',
                         message: db.label,
                         type: 'success'
                     }
                 };
                 installok("M_672", body, function () {
                     if (db && db.AppsInitFunctions) {
                         value.AppsInitFunctions = db.AppsInitFunctions;
                         cache.unlinkall(function () {
                             setMessage(value)
                         })
                     } else {
                         cache.unlinkall(function () {
                             setMessage(value)
                         })
                     }
                 })
             } else {
                 var value = {
                     error: 0
                 };
                 installok("M_672", body, function () {
                     cache.unlinkall(function () {
                         setMessage(value)
                     })
                 })
             }
         })
     }
     var router = function (req, res, next) {
         var body = UserControl.GetForm(req.session.key, req.body);
          
         new source("M_671", body, function (obj) {
             if (obj) {
                 new unInstallApp(obj, body);
             }
             var value = {
                 error: 0
             };
             res.Json(value)
         })

     };

     return router;
 }